<?php

/**
 * 每月1号凌晨系统生成个人账户提现申请单
 * 需要检查用户是否签订协议
 * 检查上个月用户的提现申请单有没有入账，如果没有入账，则上月提现申请单失效，并返回提现金额到用户可申请提现金额。并在本月申请单中累计
 * @author tangyiwan
 */


ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\salesRebate\SalesAccount as mSalesAccount;
use Biz\model\salesRebate\SalesPersonal as mSalesPersonal;
use Biz\service\salesRebate\SalesWithdrawal as sSalesWithdrawal;
use Doraemon\amqp\Queue;

$list = mSalesAccount::getAllByWhere('*', ['type' => mSalesAccount::TYPE_PERSONAL, 'balance[>]' => 0]);
$uids = array_column($list, 'uid');
$personalInfos = mSalesPersonal::getAllByWhere('*', ['uid' => $uids, 'status' => mSalesPersonal::STATUS_SIGNED]);
$uids = array_column($personalInfos, NULL, 'uid');
foreach ($list as $row) {
    if (in_array($row['uid'], array_keys($uids))) {
        $result = sSalesWithdrawal::createPersonalBill($row['uid'], $uids[$row['uid']], true);
        if (!$result) {
            $msg = '系统生成个人账户提现申请单失败#' . '#uid:' . $uid . '#widthdrawal_amount:' . $row['balance'] . '#isSys:' . true . '#msg:' . json_encode([
                    'e_file' => $e->getFile(),
                    'e_line' => $e->getLine(),
                    'e_msg' => $e->getMessage()
                ], JSON_UNESCAPED_UNICODE);
            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_RD_SYSTEM,
                "content" => $msg
            ], Queue::QUEUE_BIZ);
            Log::debug($msg);
        }
    }
}







